#include "visualize.h"

#include "dprint.h"

#include <string>

#define m_num_colors 1073
const char* m_X11_Colors[m_num_colors] =
{
    "#ff0000", "#8c00ff", "#73d8e6", "#b8cc99", "#b3a086", "#992900", "#ff4040", "#a940ff", "#00c3e6", "#a2cc66", "#b27d2d", "#997e73", "#ff8080",
    "#ce80ff", "#00bbe6", "#70cc00", "#b27100", "#994b26", "#ffbfbf", "#a600ff", "#73d0e6", "#85cc33", "#b29259", "#99654d", "#ff5040", "#bf40ff",
    "#acdbe6", "#55cc00", "#b2842d", "#993300", "#ff8a80", "#bf00ff", "#00b4e6", "#91cc66", "#b27a00", "#995026", "#ffc6bf", "#efbfff", "#00ace6",
    "#6ecc33", "#b3a486", "#99694d", "#ff5c40", "#e180ff", "#73c9e6", "#adcc99", "#b29859", "#998173", "#ff9380", "#d640ff", "#00a4e6", "#7ecc66",
    "#b28d2d", "#993b00", "#ff2f00", "#d900ff", "#acd5e6", "#29cc00", "#b28300", "#995626", "#ffcbbf", "#ec40ff", "#009de6", "#52cc33", "#b28c00",
    "#996c4d", "#ff6940", "#f480ff", "#73c1e6", "#9fcc99", "#b2952d", "#994200", "#ff9b80", "#ee00ff", "#0095e6", "#66cc66", "#b29f59", "#998373",
    "#ffcfbf", "#fdbfff", "#008ee6", "#66cc77", "#b3aa86", "#995c26", "#ff4400", "#ff00f6", "#73bae6", "#99cca2", "#b29500", "#99704d", "#ff7340",
    "#ff40f9", "#acd0e6", "#00cc2c", "#b29e2d", "#994a00", "#ffa280", "#ff80f4", "#0086e6", "#66cc86", "#b2a759", "#996226", "#ff7c40", "#ff00e1",
    "#007ee6", "#00cc47", "#b2a100", "#998773", "#ffa880", "#ff40e5", "#73b2e6", "#99ccad", "#b2aa2d", "#995200", "#ffd3bf", "#ffbff4", "#accbe6",
    "#00cc5c", "#b3b086", "#99754d", "#ff5500", "#ff00d0", "#0077e6", "#66cc94", "#b2ad00", "#996726", "#ff8640", "#ff80e5", "#73aae6", "#00cc6d",
    "#b2b059", "#995900", "#ffae80", "#ff40d6", "#006be6", "#66cca0", "#b0b32d", "#997a4d", "#ffd7bf", "#ff00bf", "#acc6e6", "#99ccb6", "#aab300",
    "#996d26", "#ff6200", "#ff40c6", "#0060e6", "#00cc7e", "#aab359", "#998a73", "#ff8c40", "#ff00ae", "#73a3e6", "#66ccac", "#a3b32d", "#996100",
    "#ffb580", "#ff80d7", "#0054e6", "#00cc8f", "#9bb300", "#99804d", "#ffdabf", "#ffbfea", "#739be6", "#99ccbe", "#adb386", "#996900", "#ff6f00",
    "#ff009d", "#acc0e6", "#00cc9c", "#89b300", "#997526", "#ff9340", "#ff40b6", "#0049e6", "#66ccb6", "#93b32d", "#997000", "#ff7700", "#ff80c8",
    "#7393e6", "#00ccaa", "#9cb359", "#998f73", "#ff9940", "#ff008c", "#003de6", "#99ccc5", "#77b300", "#997c26", "#ffbb80", "#ff40a6", "#acbae6",
    "#66ccc0", "#84b32d", "#99864d", "#ffdebf", "#ffbfe0", "#738ce6", "#00ccb8", "#a3b386", "#997800", "#ff8000", "#ff0080", "#002ee6", "#00ccc2",
    "#8db359", "#998426", "#ffa340", "#ff4099", "#acb5e6", "#66ccca", "#62b300", "#998200", "#ffc180", "#ff80bb", "#7382e6", "#00cccc", "#72b32d",
    "#998e4d", "#ff8800", "#ff0073", "#001be6", "#99cccc", "#47b300", "#999473", "#ffe2bf", "#ff0066", "#7378e6", "#00c2cc", "#7db359", "#998e26",
    "#ff9100", "#ff408c", "#acaee6", "#66c5cc", "#98b386", "#998c00", "#ffac40", "#ff80ae", "#4539e6", "#00b8cc", "#5bb32d", "#99954d", "#ffc880",
    "#ffbfd7", "#7e73e6", "#99c6cc", "#6bb359", "#999600", "#ff9900", "#ff0059", "#b6ace6", "#66becc", "#1bb300", "#979926", "#ffb640", "#ff4080",
    "#6739e6", "#00adcc", "#8bb386", "#989973", "#ffce80", "#ff004d", "#9173e6", "#00a7cc", "#59b35f", "#8f9900", "#ffa200", "#ff80a4", "#4500e6",
    "#66b8cc", "#86b38f", "#93994d", "#ffe8bf", "#ff4073", "#c1ace6", "#00a0cc", "#00b32a", "#8a9926", "#ffaa00", "#ff0040", "#8139e6", "#99c1cc",
    "#59b36e", "#7f9900", "#ffbf40", "#ffbfce", "#a473e6", "#0099cc", "#00b341", "#87994d", "#ffd780", "#ff0033", "#6700e6", "#66b1cc", "#59b37b",
    "#7c9926", "#ffb300", "#ff4066", "#9839e6", "#0092cc", "#86b398", "#709900", "#ffc940", "#ff8099", "#8200e6", "#008bcc", "#00b353", "#8f9973",
    "#ffeebf", "#ff0026", "#ceace6", "#99bccc", "#59b387", "#5e9900", "#ffbf00", "#ff4059", "#b873e6", "#66aacc", "#00b365", "#6d9926", "#ffdf80",
    "#ff808e", "#9900e6", "#0085cc", "#86b3a0", "#7a994d", "#ffd240", "#ffbfc7", "#af39e6", "#007ecc", "#59b392", "#4a9900", "#ffcc00", "#ff0015",
    "#b000e6", "#66a3cc", "#00b374", "#859973", "#ffe880", "#ff404d", "#cb73e6", "#0077cc", "#00b383", "#5c9926", "#ffdf40", "#e50000", "#c339e6",
    "#99b7cc", "#59b39c", "#6c994d", "#fff4bf", "#e53939", "#daace6", "#0070cc", "#86b3a7", "#2e9900", "#ffd900", "#e57373", "#c700e6", "#669ccc",
    "#00b38f", "#479926", "#ffe600", "#e6acac", "#d739e6", "#0066cc", "#59b3a5", "#5c994d", "#ffec40", "#e54839", "#de73e6", "#99b2cc", "#00b39b",
    "#7a9973", "#fff280", "#e57c73", "#de00e6", "#6696cc", "#86b3ae", "#4d9954", "#fff200", "#e6b2ac", "#e6ace3", "#005ccc", "#00b3a7", "#00991a",
    "#fffcbf", "#e55339", "#e600d6", "#0052cc", "#59b3ae", "#739979", "#fff940", "#e58473", "#e639d7", "#668fcc", "#00b3b3", "#4d9961", "#fffd80",
    "#e52a00", "#e673da", "#99adcc", "#00aab3", "#009930", "#ffff00", "#e6b7ac", "#e600c3", "#0047cc", "#59aeb3", "#739981", "#f5ff40", "#e55f39",
    "#e639c6", "#6688cc", "#86b0b3", "#4d996c", "#eeff00", "#e58c73", "#e600b4", "#003dcc", "#00a1b3", "#009942", "#f2ff80", "#e6baac", "#e673cb",
    "#99a7cc", "#59a7b3", "#009952", "#f8ffbf", "#e53d00", "#e6acd8", "#6681cc", "#0098b3", "#4d9977", "#ddff00", "#e56739", "#e600a4", "#0030cc",
    "#86abb3", "#739989", "#e5ff40", "#e59173", "#e639b5", "#6679cc", "#008fb3", "#009961", "#c8ff00", "#e57039", "#e60095", "#0022cc", "#59a1b3",
    "#4d9981", "#e3ff80", "#e59773", "#e673bd", "#99a2cc", "#0086b3", "#00996e", "#d2ff40", "#e6beac", "#e639a6", "#6670cc", "#599bb3", "#73998f",
    "#b2ff00", "#e54d00", "#e6accf", "#000ecc", "#0080b3", "#00997a", "#ebffbf", "#e57839", "#e50086", "#999ccc", "#86a6b3", "#4d998a", "#bfff40",
    "#e59d73", "#e63998", "#6668cc", "#007ab3", "#009985", "#d2ff80", "#e55800", "#e673b0", "#4233cc", "#5995b3", "#4d9993", "#99ff00", "#e6c2ac",
    "#e50077", "#a199cc", "#0074b3", "#739996", "#a9ff40", "#e57e39", "#e5006b", "#7766cc", "#86a1b3", "#00998f", "#bfff80", "#e5a373", "#e5398a",
    "#6133cc", "#006bb3", "#009999", "#7bff00", "#e56300", "#e673a4", "#4400cc", "#598fb3", "#4d9899", "#ddffbf", "#e58439", "#e6acc5", "#8a66cc",
    "#0062b3", "#009199", "#8fff40", "#e6c6ac", "#e50060", "#ac99cc", "#5989b3", "#739699", "#55ff00", "#e56b00", "#e5397e", "#7a33cc", "#869db3",
    "#008a99", "#aaff80", "#e5a873", "#e50054", "#6300cc", "#0059b3", "#4d9199", "#73ff40", "#e58d39", "#e53973", "#9b66cc", "#5983b3", "#008299",
    "#ceffbf", "#e57300", "#e67399", "#b799cc", "#0050b3", "#4d8b99", "#93ff80", "#e5ae73", "#e50049", "#7a00cc", "#8698b3", "#739299", "#bfffc2",
    "#e6caac", "#e6acbc", "#9133cc", "#0047b3", "#007a99", "#00ff15", "#e57a00", "#e5003d", "#ac66cc", "#597db3", "#007399", "#7fff8a", "#e59539",
    "#e53967", "#9200cc", "#003eb3", "#4d8599", "#7fff9b", "#e58200", "#e5738e", "#a633cc", "#5977b3", "#006b99", "#bfffcf", "#e5b473", "#e50032",
    "#a700cc", "#8694b3", "#738e99", "#00ff44", "#e59e39", "#e5395c", "#c399cc", "#0036b3", "#4d8099", "#7fffac", "#e58a00", "#e50026", "#bd66cc",
    "#5970b3", "#006399", "#00ff5e", "#e6cfac", "#e57384", "#ba33cc", "#002ab3", "#005c99", "#bfffdb", "#e59100", "#e6acb5", "#bb00cc", "#868fb3",
    "#4d7a99", "#00ff73", "#e5a639", "#e53950", "#cc00c9", "#001eb3", "#738999", "#80ffbb", "#e5bb73", "#e50017", "#cc33c9", "#5968b3", "#005499",
    "#00ff88", "#e59d00", "#e53945", "#cc66c9", "#5961b3", "#4d7599", "#80ffca", "#e5af39", "#cc0000", "#cc99c8", "#868ab3", "#004d99", "#bfffe4",
    "#e6d3ac", "#cc3333", "#cc00b8", "#000cb3", "#738599", "#00ff99", "#e5c373", "#cc6666", "#cc33ba", "#5c59b3", "#4d7099", "#00ffaa", "#e5a800",
    "#cc9999", "#cc66bb", "#3a2db3", "#004599", "#80ffd7", "#e5b839", "#cc6f66", "#cc00a7", "#8a86b3", "#003d99", "#00ffbb", "#e5cb73", "#cc4233",
    "#cc33ab", "#6e59b3", "#4d6b99", "#bfffee", "#e5b400", "#cc9e99", "#cc0096", "#552db3", "#738199", "#80ffe3", "#e6d9ac", "#cc7566", "#cc99be",
    "#3c00b3", "#003699", "#00ffcc", "#e5c339", "#cc4d33", "#cc66ad", "#9586b3", "#4d6599", "#00ffd9", "#e5bf00", "#cc2500", "#cc339c", "#7e59b3",
    "#002b99", "#80ffee", "#e5d473", "#cca299", "#cc0088", "#6b2db3", "#737c99", "#bffff6", "#e5cf39", "#cc7c66", "#cc007a", "#5600b3", "#4d5e99",
    "#00ffe5", "#e5cb00", "#cc5733", "#cc338f", "#9f86b3", "#002199", "#00fff2", "#e6e0ac", "#cc3600", "#cc66a2", "#8f59b3", "#4d5899", "#80fff9",
    "#e5d600", "#cca799", "#cc006d", "#6e00b3", "#001499", "#00ffff", "#e5da39", "#cc5e33", "#cc99b4", "#7f2db3", "#737799", "#bfffff", "#e5de73",
    "#cc8366", "#cc3382", "#8300b3", "#4d5099", "#80fbff", "#e5e600", "#cc4400", "#cc005f", "#912db3", "#000399", "#00f2ff", "#e5e639", "#cc6633",
    "#cc6696", "#9f59b3", "#757399", "#00e6ff", "#e0e673", "#cc8866", "#cc3375", "#aa86b3", "#2e2699", "#80f2ff", "#e3e6ac", "#ccaa99", "#cc0055",
    "#9500b3", "#574d99", "#bff9ff", "#d6e600", "#cc4e00", "#cc004b", "#a32db3", "#472699", "#00ddff", "#d4e639", "#cc6e33", "#cc668b", "#a700b3",
    "#7e7399", "#00d5ff", "#c3e600", "#cc8d66", "#cc99ac", "#b059b3", "#300099", "#80eaff", "#d0e673", "#ccad99", "#cc3369", "#b300aa", "#664d99",
    "#00ccff", "#c3e639", "#cc5800", "#cc0041", "#b32daa", "#470099", "#bff2ff", "#b0e600", "#cc7533", "#cc335e", "#b386ae", "#5c2699", "#00c4ff",
    "#d7e6ac", "#cc9266", "#cc0036", "#b3009b", "#744d99", "#80e1ff", "#b2e639", "#cc5f00", "#cc6681", "#b359a7", "#877399", "#00bbff", "#9de600",
    "#ccb199", "#cc002c", "#b32d9a", "#5c0099", "#00b3ff", "#c1e673", "#cc7d33", "#cc3354", "#b3008c", "#6d2699", "#80d9ff", "#86e600", "#cc9766",
    "#cc99a4", "#b3599b", "#824d99", "#bfecff", "#9ee639", "#cc6600", "#cc0022", "#b32d8d", "#6e0099", "#00aaff", "#b0e673", "#cc6d00", "#cc6677",
    "#b3007d", "#7e2699", "#00a2ff", "#cbe6ac", "#cc8533", "#cc334a", "#b386a5", "#917399", "#80d0ff", "#6be600", "#cc9c66", "#cc0014", "#b30071",
    "#800099", "#bfe7ff", "#87e639", "#ccb499", "#cc3340", "#b32d7f", "#904d99", "#0099ff", "#9de673", "#cc7400", "#cc666f", "#b3598f", "#8f2699",
    "#80caff", "#45e600", "#cc8c33", "#b20000", "#b20065", "#910099", "#0091ff", "#6de639", "#cca366", "#b22d2d", "#b32d72", "#99008f", "#0088ff",
    "#bde6ac", "#cc7e00", "#b25959", "#b3869d", "#99268f", "#bfe1ff", "#88e673", "#ccb899", "#b38686", "#b20059", "#994d93", "#80c1ff", "#aee6ac",
    "#cc9433", "#b26159", "#b35984", "#997396", "#007fff", "#73e67a", "#cc8800", "#b23a2d", "#b2004d", "#990080", "#0077ff", "#00e626", "#ccaa66",
    "#b38b86", "#b22d67", "#992682", "#80b9ff", "#ace6b8", "#cc9c33", "#b26859", "#b20041", "#994d87", "#bfdcff", "#73e68c", "#cc9200", "#b22100",
    "#b3597a", "#990073", "#006aff", "#00e645", "#ccbe99", "#b2452d", "#b22d5b", "#99738e", "#80b0ff", "#73e69b", "#cca333", "#b39086", "#b38695",
    "#992675", "#005dff", "#ace6c2", "#ccb166", "#b26e59", "#b20039", "#990066", "#bfd7ff", "#00e65c", "#cc9c00", "#b24e2d", "#b20030", "#994d7d",
    "#0051ff", "#73e6a8", "#ccad33", "#b23000", "#b22d50", "#990059", "#80a8ff", "#00e66f", "#cca700", "#b2552d", "#b35971", "#992669", "#0044ff",
    "#ace6cc", "#ccb966", "#b27459", "#b20027", "#99004f", "#bfd0ff", "#00e682", "#ccc499", "#b39386", "#b22d47", "#994d73", "#809fff", "#73e6b6",
    "#ccb100", "#b23c00", "#b3868f", "#997386", "#0033ff", "#00e695", "#ccb833", "#b25b2d", "#b25968", "#99265e", "#8097ff", "#73e6c1", "#ccc266",
    "#b27859", "#b2001b", "#990045", "#bfcaff", "#ace6d4", "#ccbb00", "#b39686", "#b22d3e", "#992654", "#001eff", "#00e6a4", "#ccc233", "#b24400",
    "#b2000f", "#99003b", "#808cff", "#00e6b4", "#ccca99", "#b2622d", "#b22d36", "#994d6a", "#bfc2ff", "#73e6cd", "#ccc900", "#b27d59", "#990000",
    "#990030", "#8082ff", "#ace6dc", "#cccc66", "#b24d00", "#992626", "#99264b", "#4d40ff", "#00e6c3", "#c9cc33", "#b39986", "#994d4d", "#99737f",
    "#c8bfff", "#73e6d8", "#becc00", "#b2692d", "#997373", "#994d61", "#9380ff", "#00e6cf", "#bacc33", "#b28159", "#993226", "#990026", "#7340ff",
    "#00e6da", "#becc66", "#b25600", "#99544d", "#992641", "#4800ff", "#73e6e2", "#adcc00", "#b2702d", "#997773", "#99001c", "#a880ff", "#ace6e4",
    "#c4cc99", "#b28659", "#991a00", "#994d59", "#d5bfff", "#00e6e6", "#abcc33", "#b39c86", "#993b26", "#997379", "#8f40ff", "#00dae6", "#9ccc00",
    "#b25f00", "#995b4d", "#992637", "#6f00ff", "#73e0e6", "#b1cc66", "#b2762d", "#997b73", "#990012", "#bb80ff", "#ace1e6", "#88cc00", "#b28c59",
    "#994326", "#992630", "#e1bfff", "#00cfe6", "#99cc33", "#b26800", "#99604d"
};

std::string GraphViz_Visualizer::get_color_for_id(uint64_t pID)
{
    std::string returnStr;

    if (pID && (settings->color_identities->get_value() == on))
    {
        returnStr = " BGCOLOR=\"";
        auto iter = m_identity_colors.find(pID);
        if (iter != m_identity_colors.end())
        {
            uint64_t lIDColor = iter->second;
            assert(lIDColor < m_num_colors);
            //dprint(DT_DEBUG, "Returning color %s for %u.\n", m_X11_Colors[iter->second], pID);
            returnStr += m_X11_Colors[iter->second];
        } else {
            m_identity_colors[pID] = m_next_color;
            if (++m_next_color == m_num_colors) m_next_color = 1;
            //dprint(DT_DEBUG, "Returning new color %s for %u.\n", m_X11_Colors[m_next_color-1], pID);
            returnStr += m_X11_Colors[m_next_color-1];
        }
        returnStr += "\" ";
    }
    else returnStr = " ";

    return returnStr;
}
